package com.makingTeam.config.mqttAndNetty;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author pzy
 *配置管道  服务端初始化，客户端与服务器端连接一旦创建，这个类中方法就会被回调，设置出站编码器和入站解码器
 *
 */
@Component
public class MqttServerChannelInitializer extends ChannelInitializer<SocketChannel> {

    @Autowired
    private BootNettyMqttChannelInboundHandler bootNettyMqttChannelInboundHandler;

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {

        ChannelPipeline p = socketChannel.pipeline();
        //设置超时时间
//        p.addLast(new IdleStateHandler(30, 30, 60, TimeUnit.SECONDS));
        p.addLast(new LoggingHandler(LogLevel.INFO));
        p.addLast(new ServerMqttHandler());
        //这里设置读取报文的包头长度来避免粘包
//        p.addLast(new LengthFieldPrepender(2));
        //通用处理器
        p.addLast(bootNettyMqttChannelInboundHandler);

    }
}
